require "proto/friend/message/ReqAddFriendMessage"
require "proto/friend/message/ReqAgreeFriendMessage"
require "proto/friend/message/ReqGiveFriendshipMessage"
require "proto/friend/message/ReqReceiveFriendshipMessage"
require "proto/friend/message/ReqRefuseFriendMessage"
require "proto/friend/message/ReqRemoveFriendMessage"
require "proto/friend/message/ReqRecommendMessage"
require "proto/friend/message/ReqSearchIdMessage"
require "proto/friend/message/ReqSearchNameMessage"
require "proto/friend/message/ReqFriendDataMessage"

require "proto/friend/message/ReqAllFriendGiftListMessage"
require "proto/friend/message/ReqGetFriendGiftMessage"
require "proto/friend/message/ReqGetFriendGiftSocialBoxMessage"
require "proto/friend/message/ReqGetFriendGiftBigBoxMessage"
require "proto/friend/message/ReqDeleteFriendGiftMessage"
require "proto/friend/message/ReqFriendCompeteInfoMessage"
require "proto/friend/message/ReqFriendCompeteBattleMessage"
require "proto/friend/message/ReqFriendCompeteHistoryMessage"
require "proto/player/message/ReqBlackPLayerMessage"
require "proto/player/message/ReqBlackListMessage"

friendHandler = friendHandler or {};
G.serverData = G.serverData or {};
--region 好友
function friendHandler.ReqFriendData()
    local msg = ReqFriendDataMessage.New()
    sendReqMessage(msg)
end
function friendHandler.receiveFriendsHandler(msg)
    logicMgr.FriendManager.ClearFriend()
    if msg.state == 0 then
        local friends = msg.friends; 
        for i=1,#friends do
            FriendManager.AddFriend(EnumConst.FriendType.myFriend,friends[i])
        end

        local receives = msg.receives; 
        for i=1,#receives do
            logicMgr.FriendManager.SetFriendGetLoveState(receives[i],EnumConst.LoveStateEnum.NORMAL)
        end
        local received = msg.received;
        for i=1,#received do
            logicMgr.FriendManager.SetFriendGetLoveState(received[i],EnumConst.LoveStateEnum.USED)
        end
        local gived = msg.gived; 
        for i=1,#gived do
            logicMgr.FriendManager.SetFriendSendLoveState(gived[i],EnumConst.LoveStateEnum.USED)
        end

        local applys = msg.applys; 
        for i=1,#applys do
            logicMgr.FriendManager.AddFriend(EnumConst.FriendType.applyFriend,applys[i])
        end
        logicMgr.FriendManager.SetFriendShip(msg.friendship);
        logicMgr.FriendManager.SetTodayFriendShip(msg.friendshipAmount)
        FireEvent(Config.EventType.Fresh_MyFriends);
        FireEvent(Config.EventType.Fresh_ApplyFriend);
        FireEvent(Config.EventType.FriendRedPoint);
    end
end

function friendHandler.receiveAddFriendHandler(msg)
    if msg.state == 0 then
        logicMgr.FriendManager.DeleteFriend(EnumConst.FriendType.find,msg.friendId)
        logicMgr.FriendManager.DeleteFriend(EnumConst.FriendType.recommend,msg.friendId)
        FireEvent(Config.EventType.Fresh_RecommendFriend);
        FireEvent(Config.EventType.FriendRedPoint);
        --UISysTips.AddMsg("已发送申请")
    end
end

function friendHandler.receiveAddFriendSuccessHandler(msg)
    if msg.state == 0 then
        logicMgr.FriendManager.AddFriend(EnumConst.FriendType.myFriend,msg.friend)
        logicMgr.FriendManager.SetFriendSendLoveState(msg.friend.playerId, msg.gived == 1 and EnumConst.LoveStateEnum.USED or EnumConst.LoveStateEnum.NORMAL)

        if msg.received == 1 then
            logicMgr.FriendManager.SetFriendGetLoveState(msg.friend.playerId, EnumConst.LoveStateEnum.NORMAL)
        elseif msg.received ~= nil and msg.received == 2 then
            logicMgr.FriendManager.SetFriendGetLoveState(msg.friend.playerId, EnumConst.LoveStateEnum.USED)
        else
            logicMgr.FriendManager.SetFriendGetLoveState(msg.friend.playerId, EnumConst.LoveStateEnum.NONE)
        end

        logicMgr.FriendManager.DeleteFriend(EnumConst.FriendType.applyFriend,msg.friend.info.playerId)
        FireEvent(Config.EventType.Fresh_ApplyFriend);
        FireEvent(Config.EventType.Fresh_MyFriends);
        FireEvent(Config.EventType.FriendRedPoint);
    end
end

function friendHandler.receiveAgreeFriendHandler(msg)
	if  msg.state ==0 then
        local mgr = logicMgr.FriendManager
        local friendInfo = mgr.GetFriend(EnumConst.FriendType.applyFriend,msg.friendId)
        mgr.AddFriend2(EnumConst.FriendType.myFriend,friendInfo)
        mgr.DeleteFriend(EnumConst.FriendType.applyFriend,msg.friendId)
        FireEvent(Config.EventType.Fresh_ApplyFriend);
        FireEvent(Config.EventType.FriendRedPoint);
        UISysTips.AddMsg(getLanguage("UIFriend_ApplyAgree"))
    end
end
function friendHandler.FriendUpdateHandler(msg)
    if msg.state ~= 0 then return end
    local mgr = logicMgr.FriendManager
    mgr.UpdateFriend(EnumConst.FriendType.myFriend,msg.friendInfo)

    --FireEvent(Config.EventType.Fresh_MyFriends);
    --FireEvent(Config.EventType.FriendRedPoint);
end
function friendHandler.receiveRefuseFriendHandler(msg)
	if  msg.state ==0 then
        logicMgr.FriendManager.DeleteFriend(EnumConst.FriendType.applyFriend,msg.friendId)
        FireEvent(Config.EventType.Fresh_ApplyFriend);
        FireEvent(Config.EventType.FriendRedPoint);
    end
end
--被申请好友返回
function friendHandler.receiveApplyFriendHandler(msg)
    if msg.state == 0 then
        logicMgr.FriendManager.AddFriend(EnumConst.FriendType.applyFriend,msg.friend)
        FireEvent(Config.EventType.Fresh_ApplyFriend);
        FireEvent(Config.EventType.FriendRedPoint);
    end
end

function friendHandler.receiveRemoveFriendHandler(msg)
    print("receiveRemoveFriendHandler");
end

function friendHandler.receiveRemoveFriendSuccessHandler(msg)
    if  msg.state ==0 then
        logicMgr.FriendManager.DeleteFriend(EnumConst.FriendType.myFriend,msg.friendId)
        FireEvent(Config.EventType.Fresh_MyFriends);
        FireEvent(Config.EventType.FriendRedPoint);
    end
end
--赠送友情点返回
function friendHandler.receiveGiveFriendshipHandler(msg)
    if msg.state == 0 then
        local friends = msg.friendIds; 
        for i=1,#friends do
            logicMgr.FriendManager.SetFriendSendLoveState(friends[i],EnumConst.LoveStateEnum.USED)
        end
        FireEvent(Config.EventType.Fresh_MyFriends);
        FireEvent(Config.EventType.FriendRedPoint);
        UISysTips.AddMsg("友情点赠送成功")
    end
end
--收取友情点返回
function friendHandler.ResReceiveFriendshipHandler(msg)
    if msg.state == 0 then
        local friends = msg.friendIds; 
        for i=1,#friends do
            logicMgr.FriendManager.SetFriendGetLoveState(friends[i],EnumConst.LoveStateEnum.USED)
        end
        logicMgr.FriendManager.SetFriendShip(msg.friendship)
        logicMgr.FriendManager.SetTodayFriendShip(FriendManager.GetTodayFriendShip() + msg.amount)
        FireEvent(Config.EventType.Fresh_MyFriends);
        FireEvent(Config.EventType.FriendRedPoint);
        UISysTips.AddMsg("友情点领取成功")
    end
end
--赠送友情点返回(通知对方)
function friendHandler.receiveGiveFriendshipSuccessHandler(msg)
    if msg.state == 0 then
        logicMgr.FriendManager.SetFriendGetLoveState(msg.friendId,EnumConst.LoveStateEnum.NORMAL)
        FireEvent(Config.EventType.Fresh_MyFriends);
        FireEvent(Config.EventType.FriendRedPoint);
    end
end

function friendHandler.sendAddFriend(friendId,leaveMsg)
    local req = ReqAddFriendMessage.New();
    req.friendId = friendId;
    req.leaveMsg = leaveMsg or ""
    sendReqMessage(req);
    UISysTips.AddMsg(getLanguage("AppFriend_Tip"))
end

function friendHandler.sendRemoveFriend(friendId)
    local req = ReqRemoveFriendMessage.New();
    req.friendId = friendId;
    sendReqMessage(req);
end

function friendHandler.sendAgreeFriend(friendId)
    local req = ReqAgreeFriendMessage.New();
    req.friendId = friendId;
    sendReqMessage(req);
end

function friendHandler.sendRefuseFriend(friendId)
    local req = ReqRefuseFriendMessage.New();
    req.friendId = friendId;
    sendReqMessage(req);
end
--赠送友情点
function friendHandler.sendGiveFriendship(friendIds)
    local req = ReqGiveFriendshipMessage.New();
    req.friendIds = friendIds;
    sendReqMessage(req);
end
--领取友情点
function friendHandler.sendReceiveFriendship(friendIds)
    local req = ReqReceiveFriendshipMessage.New();
    req.friendIds = friendIds;
    sendReqMessage(req);
end

function friendHandler.getFriendRecommend()
    local req = ReqRecommendMessage.New();
    local _list = {}
    local recommends = logicMgr.FriendManager.GetFriendList(EnumConst.FriendType.recommend)
    if recommends then
        for k,v in pairs(recommends) do
            table.insert(_list,v.playerId)
        end
    end
    req.recommandeds = _list;
    sendReqMessage(req);
end

function friendHandler.ResgetFriendRecommend(msg)
    local v = msg.recommends;
    logicMgr.FriendManager.ClearRecommend()
    if v and #v > 0 then
        for i=1,#v do
            logicMgr.FriendManager.AddFriend(EnumConst.FriendType.recommend,v[i])
        end
    end
    FireEvent(Config.EventType.Fresh_RecommendFriend);
end

function friendHandler.findFriendByName(name)
    local paramdata = {playerId = logicMgr.HeroManager.GetRoleInfo().playerId};
    paramdata.friendName = name;
    if friendHandler.CheckIsMySelf(name) then
        UISysTips.AddMsg("请勿添加自己")
        return
    end
    local req = ReqSearchNameMessage.New();
    req.friendName = name;
    sendReqMessage(req);
end
function friendHandler.findFriendByID(friendId)
    local _playerId = logicMgr.HeroManager.GetRoleInfo().playerId
    if _playerId == friendId then
        UISysTips.AddMsg("请勿添加自己");
        return
    end
    local req = ReqSearchIdMessage.New();
    req.friendId = friendId;
    sendReqMessage(req);
end
function friendHandler.ResFindFriendName(msg)
    logicMgr.FriendManager.ClearFriendByType(EnumConst.FriendType.find)
    if msg.state == 0 then
        local friend = msg.friendInfo[1]
        logicMgr.FriendManager.AddFriend(EnumConst.FriendType.find,friend)
    end
    FireEvent(Config.EventType.Fresh_RecommendFriend);
end

function friendHandler.ResFindFriendByID(msg)
    logicMgr.FriendManager.ClearFriendByType(EnumConst.FriendType.find)
    if msg.state == 0 then
        local friend = msg.friendInfo[1]
        logicMgr.FriendManager.AddFriend(EnumConst.FriendType.find,friend)
    end
    FireEvent(Config.EventType.Fresh_RecommendFriend);
end

function friendHandler.CheckIsMySelf(name)
    if logicMgr.HeroManager.GetRoleId() == name or logicMgr.HeroManager.GetRoleInfo().name == name then
        return true;
    end
    return false
end

--endregion 好友
--region 好友礼包
function friendHandler.ReqAllFriendGiftList()
   local m = ReqAllFriendGiftListMessage.New()
    sendReqMessage(m)
end
function friendHandler.ReqGetFriendGift(id)
    local m = ReqGetFriendGiftMessage.New()
    m.giftId = id or 0
    sendReqMessage(m)
end
function friendHandler.ReqGetFriendGiftSocialBox()
    local m = ReqGetFriendGiftSocialBoxMessage.New()
    sendReqMessage(m)
end
function friendHandler.ReqGetFriendGiftBigBox()
    local m = ReqGetFriendGiftBigBoxMessage.New()
    sendReqMessage(m)
end
function friendHandler.ReqDeleteFriendGift()
    local m = ReqDeleteFriendGiftMessage.New()
    sendReqMessage(m)
end
function friendHandler.ResAllFriendGiftListHandler(msg)
    if msg.state == 0 then
        logicMgr.FriendManager.UpdateFriendGift(msg.level,msg.exp,msg.pointNum)
        logicMgr.FriendManager.UpdateFriendGiftSocialBoxLevel(msg.socialBoxLevel)
        for k,v in pairs(msg.infos) do
            logicMgr.FriendManager.AddFriendGiftInfo(v)
        end
    end
    FireEvent(Config.EventType.Fresh_FriendGiftInfo);
end
function friendHandler.ResUpdateFriendGiftHandler(msg)
    if msg.state == 0 then
        logicMgr.FriendManager.UpdateFriendGift(msg.level,msg.exp,msg.pointNum)
        for k,v in pairs(msg.add) do
            logicMgr.FriendManager.AddFriendGiftInfo(v)
        end
        for k,v in pairs(msg.update) do
            logicMgr.FriendManager.UpdateFriendGiftInfo(v)
        end
        for k,v in pairs(msg.delete) do
            logicMgr.FriendManager.DeleteFriendGiftInfo(v)
        end
        FireEvent(Config.EventType.Fresh_FriendGiftInfo);
        if #msg.add > 0 then
            UISysTips.AddMsg(getLanguage("Sys_Friend_Gift_Get_Gift_Tip"))
        end
    end
end
--返回领取社交终极宝箱
function friendHandler.ResGetFriendGiftBigBoxHandler(msg)
    if msg.state == 0 then
        logicMgr.UIShowManager.ShowRewardByTempItemInfo(msg.items)
    end
end
--返回领取好友礼包
function friendHandler.ResGetFriendGiftHandler(msg)
    if msg.state == 0 then
        logicMgr.UIShowManager.ShowRewardByTempItemInfo(msg.items)
    end
end
--返回领取社交等级宝箱
function friendHandler.ResGetFriendGiftSocialBoxHandler(msg)
    if msg.state == 0 then
        logicMgr.FriendManager.UpdateFriendGiftSocialBoxLevel(logicMgr.FriendManager.friendGiftLevel)
        logicMgr.UIShowManager.ShowRewardByTempItemInfo(msg.items)
        FireEvent(Config.EventType.Refresh_FriendSocialLevel)
    end
end
function friendHandler.ResDeleteFriendGiftHandler(msg)

end
--endregion 好友礼包

--好友切磋 start
--请求获得好友切磋信息
function friendHandler.ReqFriendCompeteInfo(friendId)
    local m = ReqFriendCompeteInfoMessage.New()
    m.friendId = friendId;
    sendReqMessage(m)
end
--请求获得好友切磋信息
function friendHandler.ResFriendCompeteInfoHandler(msg)
    logicMgr.FriendManager.SetFriendCompeteBattleInfo(msg)
    FireEvent(Config.EventType.FriendCompeteBattle,msg);
end

--请求获得好友切磋战斗信息
function friendHandler.ReqFriendCompeteBattle(friendId,formation,supportSkill)
    local m = ReqFriendCompeteBattleMessage.New()
    m.friendId = friendId;
    m.formation = formation;
    m.supportSkill = supportSkill;
    sendReqMessage(m)

end
--请求获得好友切磋战斗信息返回
function friendHandler.FriendCompeteBattleResultHandler(msg)
    if msg.state ~= 0 then
        logError("进入战斗错误-战斗返回失败 code:" .. msg.state)
        FireEvent(Config.EventType.LoadingCancelByFightError, "战斗返回失败")
        return
    end
    logicMgr.BattlePlaybackManager.RequestReport(msg.fightId, msg.fightResult, msg.record)
    logicMgr.BattlePlaybackManager.ResetReceiveReport()
    logicMgr.BattlePlaybackManager.GotoFight(FriendManager.GetFriendCompeteBattleType())

end

--请求获得好友切磋历史信息
function friendHandler.ReqFriendCompeteHistory()
    local m = ReqFriendCompeteHistoryMessage.New()
    sendReqMessage(m)
end

function friendHandler.ResFriendCompeteHistoryHandler(msg)
    FireEvent(Config.EventType.FriendCompeteHistory,msg)

end
--好友切磋 end

-----------黑名单 s
function friendHandler.ReqBlackPLayer(pid, type)
    local m = ReqBlackPLayerMessage.New()
    m.playerId = tostring(pid)
    m.type = type
    sendReqMessage(m)
end

function friendHandler.ReqBlackList()
    local m = ReqBlackListMessage.New()
    sendReqMessage(m)
end

function friendHandler.ResBlackPLayer(msg)
    if msg.state == 0 then
        logicMgr.FriendManager.OperateBlackPlayer(msg.playerId, msg.type)
    end
end

function friendHandler.ResBlackList(msg)
    if msg.state == 0 then
        logicMgr.FriendManager.OperateBlackPlayer(msg.playerId, msg.type)
    end
end
-----------黑名单 e